/**********************************************************************************************
Trigger Name: NMRDoNotCallLeadUpdate 
* Author: Accenture 
* Date: 02-March-2012  
* Requirement/Project Name: NMR 
* Requirement Description: When a Do Not Call record is updated to Active, the Corresponding lead
                           gets marked Do Not Call accordingly.
* Version Update:   Update added - When a DNC record is created, a check will be made on 6 fields instead of 1
                    and accordingly the Do Not call flag will be marked.                         
***********************************************************************************************/

trigger NMRDoNotCallLeadUpdate on Do_Not_Call__c (after insert, after update, before delete) {
    
    list<Do_Not_Call__c> DNC = new List<Do_Not_Call__c>();           //List of Do Not Calls records that triggered the code.
    list<Do_Not_Call__c> dncUpdated = new List<Do_Not_Call__c>();    // List of updated Do Not Call Records.
    list<Lead> leadList = new list<Lead>();                          // List of Leads matching the Phone numbers on the Do Not Call records.
    Set<String> phoneSet = new Set<String>();                        // Phone numbers of the Do Not Call Records which triggered the code.
    ID dncRecordTypeId = RecordTypeHelper.GetRecordTypeID('Do_Not_Call__c','Do Not Call');    
                
    if(!Trigger.ISDELETE){
       for(Do_Not_Call__c DNCList: trigger.New){
            NMRUtil.processed.add(DNCList.Id);
                if((trigger.isInsert && DNCList.Active__c == True) || (trigger.isUpdate &&(DNCList.Active__c != Trigger.OldMap.get(DNCList.Id).Active__c && DNCList.RecordTypeId == dncRecordTypeId))){
                    phoneSet.add(DNCList.Phone_Number__c);                       
                }
       }
    }
               else{
                   for(Do_Not_Call__c DNCList: trigger.old){
                       NMRUtil.processed.add(DNCList.Id);
                       phoneSet.add(DNCList.Phone_Number__c);                            
                   }   
               }
    NMRDoNotCall.nmrDoNotCallLeadUpdate(phoneSet);         
       
 }